package kr.ac.mlb.dao;


import java.sql.*;
import java.io.*;
import java.util.*;

import kr.ac.mlb.vo.ContentVo;
public class ListDao {

	//SOCCER 관심사 테이블에 데이터 추가
	public void Soccer(String str,String str2)
	{
		
		Connection conn = null;
		Statement stmt= null;
		
		String dburl = "jdbc:mysql://localhost:3306/test";
		String id = "root";
		String pw = "123456789";
		
		try{
			Class.forName("com.mysql.jdbc.Driver");
		}
		catch(ClassNotFoundException e){
			System.out.println("Driver Failed");
			System.out.println(e.toString());
			return;
		}
		
		try{
			conn = DriverManager.getConnection(dburl, id, pw);
//			System.out.println("Connection Success!");
		}
		catch(Exception e){
			System.out.println("Connection Failed");
			System.out.println(e.toString());
			return;
		}
		
		try {
			stmt = conn.createStatement();
			
			String sql ="insert into soccer (content,time) values ('"+str+"','"+str2+"')";
		
			
			//ResultSet rs = stmt.executeQuery(sql);
			
			stmt.executeUpdate(sql);
			//rs.close();

			
		}catch (Exception ex){
			ex.printStackTrace();
		}
	}
	
	
	public void insert(ContentVo vo){

		
		Connection conn = null;
		Statement stmt= null;
		
		String dburl = "jdbc:mysql://localhost:3306/test";
		String id = "root";
		String pw = "123456789";
		
		try{
			Class.forName("com.mysql.jdbc.Driver");
		}
		catch(ClassNotFoundException e){
			System.out.println("Driver Failed");
			System.out.println(e.toString());
			return;
		}
		
		try{
			conn = DriverManager.getConnection(dburl, id, pw);
//			System.out.println("Connection Success!");
		}
		catch(Exception e){
			System.out.println("Connection Failed");
			System.out.println(e.toString());
			return;
		}
		
		try {
			stmt = conn.createStatement();
			
			String sql = "insert into content (content, time) values ('"+vo.getContent()+"', '"+vo.getTime()+"')";
			
			//ResultSet rs = stmt.executeQuery(sql);
			
			stmt.executeUpdate(sql);
			
			//rs.close();

			
		}catch (Exception ex){
			ex.printStackTrace();
		}
	}

	public List<ContentVo> getList(){
		
		List<ContentVo> list = new ArrayList<ContentVo>();
		
		Connection conn = null;
		Statement stmt= null;
		
		String dburl = "jdbc:mysql://localhost:3306/test";
		String id = "root";
		String pw = "123456789";
		
		try{
			Class.forName("com.mysql.jdbc.Driver");
		}
		catch(ClassNotFoundException e){
			System.out.println("getList Driver Failed");
			System.out.println(e.toString());
			return null;
		}

		try{
			conn = DriverManager.getConnection(dburl, id, pw);
//			System.out.println("Connection Success!");
		}
		catch(Exception e){
			System.out.println("Connection Failed");
			System.out.println(e.toString());
			return null;
		}

		try {

			stmt = conn.createStatement();

			String sql = "select content, time from content order by con_id desc";

			ResultSet rs = stmt.executeQuery(sql);
			
			while(rs.next()){
				ContentVo vo = new ContentVo();
				vo.setContent(rs.getString(1));
				vo.setTime(rs.getString(2));

				list.add(vo);
			}
			rs.close();

		}catch (Exception ex){
			ex.printStackTrace();
		} 
		
		return list;
	}
}
